﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

using SamuelBlanchard.Extenders;
using Techdays2013.Codes.TextToSpeech;
using Windows.Phone.Speech.Synthesis;
using System.Globalization;
using SamuelBlanchard.Accessibility;

namespace Techdays2013
{
    public partial class TextToSpeechPage : PhoneApplicationPage
    {
        /// <summary>
        /// Constructeur
        /// </summary>

        public TextToSpeechPage()
        {
            InitializeComponent();

            this.LayoutRoot.DataContext = this;

            this.VoiceLanguages = TextToSpeechHelper.GetVoiceLanguages();
            
            this.SsmlSamples = new List<ListPickerItem>()
            {
                new ListPickerItem(){ Content = "Texte simple", Tag="Bonjour madame." },
                new ListPickerItem(){ Content = "Voix et langue", Tag="<voice xml:lang=\"fr-FR\" gender=\"male\">Je voudrais dire ceci à Rival Duncan :</voice><voice xml:lang=\"ja-JP\" gender=\"female\">ありがとうございました !</voice>" },
                new ListPickerItem(){ Content = "Prosody", Tag="<voice gender=\"female\">Ou étais-tu passée ?<prosody rate=\"slow\" volume=\"75.0\" pitch=\"1200Hz\">scusez moi mdame c'est à cause de la neige.</prosody></voice>" },
                new ListPickerItem(){ Content = "Censure", Tag="<voice gender=\"male\">J'ai envie de <audio src=\"Beep.wav\">travailler</audio></voice>" },
            };

            this.ToggleGender.IsChecked = InstalledVoices.Default.Gender == VoiceGender.Female;

            this.Loaded += TextToSpeechPage_Loaded;
            this.BackKeyPress += TextToSpeechPage_BackKeyPress;
            this.GridFade.Visibility = System.Windows.Visibility.Visible;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        void TextToSpeechPage_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = true;
            this.GridFade.Visibility = System.Windows.Visibility.Visible;
            this.GridFade.FadeIn((s) => { NavigationService.GoBack(); });
        }

        /// <summary>
        /// On attent sue tout soit mise à jour
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        void TextToSpeechPage_Loaded(object sender, RoutedEventArgs e)
        {
            this.GridFade.FadeOut((s) => { s.Visibility = Visibility.Collapsed; });
        }

        /// <summary>
        /// Langue disponibles
        /// </summary>

        public List<CultureInfo> VoiceLanguages
        {
            get { return (List<CultureInfo>)GetValue(VoiceLanguagesProperty); }
            set { SetValue(VoiceLanguagesProperty, value); }
        }

        // Using a DependencyProperty as the backing store for VoiceLanguages.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty VoiceLanguagesProperty =
            DependencyProperty.Register("VoiceLanguages", typeof(List<CultureInfo>), typeof(TextToSpeechPage), new PropertyMetadata(null));

        /// <summary>
        /// Samples
        /// </summary>

        public List<ListPickerItem> SsmlSamples
        {
            get { return (List<ListPickerItem>)GetValue(SsmlSamplesProperty); }
            set { SetValue(SsmlSamplesProperty, value); }
        }

        // Using a DependencyProperty as the backing store for SsmlSamples.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SsmlSamplesProperty =
            DependencyProperty.Register("SsmlSamples", typeof(List<ListPickerItem>), typeof(TextToSpeechPage), new PropertyMetadata(null));

        /// <summary>
        /// Parler
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private async void Button_Click_Talk(object sender, RoutedEventArgs e)
        {
            await TextToSpeechHelper.Speak(this.TextBoxMaPhrase.Text);
        }

        /// <summary>
        /// Changement de langue
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private async void Button_Click_LanguageGender(object sender, RoutedEventArgs e)
        {
            await TextToSpeechHelper.Speak(
                this.TextBoxMaPhrase.Text, 
                ListPickerLanguage.SelectedItem as CultureInfo,
                ToggleGender.IsChecked == true ? VoiceGender.Female : VoiceGender.Male
                );
        }

        /// <summary>
        /// Example SSML
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private async void Button_Click_SSML(object sender, RoutedEventArgs e)
        {
            await TextToSpeechHelper.SpeakSsml(this.TextBoxSsml.Text, false, "fr-FR");
        }

        /// <summary>
        /// Phrase en cours
        /// </summary>

        bool isHelloWord = true;

        /// <summary>
        /// On change de phrase
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void TextBoxMaPhrase_ActionIconTapped_1(object sender, EventArgs e)
        {
            isHelloWord = !isHelloWord;

            if (isHelloWord == true)
            {
                this.TextBoxMaPhrase.Text = "Hello Word !";
            }
            else
            {
                this.TextBoxMaPhrase.Text = "Bonjour madame !";
            }
        }
    }
}